package com.payment.notify.receive.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.payment.common.dto.notify.NotifyDto;
import com.payment.notify.receive.enums.NotifyReceiveEnum;
import com.payment.notify.receive.service.factory.NotifyReceivResolveHandler;
import com.payment.notify.receive.service.factory.NotifyReceiveResoFactory;

// Spring Restful MVC Controller的标识, 直接输出内容，不调用template引擎.
@RestController
public class NotifyReceiveEndPoint {

	private static Logger logger = LoggerFactory.getLogger(NotifyReceiveEndPoint.class);
	
	
	/**
	 * 功能描述:通过统一notifyType不同值实现不同的业务<br>
	 * 创建者：XINJF<br>
	 * 创建日期:2018年5月12日下午12:16:42<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 * @param msg
	 * @param notifyType
	 * @param response
	 */
	@RequestMapping(value = "/api/notifyReceive")
	public void notifyReceive(@RequestParam String msg,@RequestParam Integer notifyType,HttpServletResponse response) {
		PrintWriter out = null;
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		try {
			NotifyReceiveEnum notifyReceiveEnum = NotifyReceiveEnum.getEnum(notifyType);
			NotifyReceivResolveHandler factoryHandler = new NotifyReceiveResoFactory();
			NotifyDto notifyDto = factoryHandler.resolveReceiveMsg(notifyReceiveEnum, msg);
			if(notifyDto != null) {
				//操作成功后告诉消费者成功
				out = response.getWriter();
				out.write("success");
				out.close();
				logger.info("接收消息成功" + notifyType);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
